package edu.uj.petri.view;

import edu.uj.petri.commands.Command;
import edu.uj.petri.document.Document;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

/**
 *Context menu passed from diagram element to GUI.
 *
 *<h1>Usage</h1>
 *
 *<pre>// filling
 *ContextMenu menu = new ContextMenu();
 *menu.addItem(&quot;set weight&quot;, new EditWeightCommand(this));
 *menu.addItem(&quot;delete&quot;, new DeleteArcViewCommand(this));
 *return menu;
 *
 * // reading
 *ContextMenu menu = diagramElement.getContextMenu();
 *for (menu.startReading; menu.next(); menu.hasNext()) {
 *  System.out.println( menu.getCaption() );
 *  System.out.println( menu.getCommand().toString() );
 *}</pre>
 */
public class ContextMenu {
    
    private static class MyMenuItem extends JMenuItem {
        private final Command command;
        
        MyMenuItem(String caption, final Command command) {
            super( caption );
            this.command = command;
            addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    
                    Document.getActiveDocument().execute(command);
                }
                
            });
        }
        
        private static final long serialVersionUID = -229332739024460L;
    }
    
    private JPopupMenu menu = new JPopupMenu();
    
    public ContextMenu() {
        
    }
    
    public void addItem(String caption, Command command) {
        
        menu.add( new MyMenuItem( caption, command ));
    }
    
    public void addItemsFrom(ContextMenu another) {
        // TODO implement
    }
    
    public void show( Component component, int x, int y) {
        menu.show( component, x, y);
    }
    
    public void startReading() {
    }
    
    public void next() {
    }
    
    public String getCaption() {
        return null;
    }
    
    public Command getCommand() {
        return null;
    }
}
